/**
 * 0. Project	: com.dnbns.common 프로젝트
 *
 * 1. FileName	: CommonAsyncTask.java
 * 2. Package	: com.dnbns.common
 * 3. 작성자		: 윤화현
 * 4. 작성일		: 2013. 11. 19. 오후 3:51:18
 * 5. Comment	: 서버와 통신을 담당하고 있다. Handler를 통해 결과 값을 전달하게 된다.
 */

package com.dnbns.common;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.dnbns.bodywater.bean.MapBean;
import com.dnbns.lib.CodeUtil;

public class CommonAsyncTask {

	private static CommonAsyncTask mInstance;
	private Context mContext;
	private Handler mHandler;
	
	public CommonAsyncTask(Context mContext, Handler mHandler){
		this.mContext = mContext;
		this.mHandler = mHandler;
	}
	
	public static CommonAsyncTask getInstance(Context mContext, Handler mHandler){
		mInstance = new CommonAsyncTask(mContext, mHandler);	 
		return mInstance;
	}
	
	public void getWaterDataTask(JSONObject params){
		new getWaterDataTask().execute(params);
	}

	public void getMapDataTask(JSONObject params, ArrayList<MapBean> mList){
		new getMapDataTask(mList).execute(params);
	}
	
/**
 * =================================================================================
 * 서버 통신
 * =================================================================================
 */

	class getWaterDataTask extends AsyncTask<JSONObject, Void, JSONObject>{
		
		@Override
		protected JSONObject doInBackground(JSONObject... params) {
			// TODO Auto-generated method stub
			JSONObject data = CodeUtil.getInstance(mContext).getHttpClient(params[0]);
			return data;
		}
		
		@Override
		protected void onPostExecute(JSONObject result) {
			// TODO Auto-generated method stub
//			super.onPostExecute(result);
			Message msg = new Message();
			try {
				if(result == null){
					msg.what = 2000;
					mHandler.sendMessage(msg);
					return;
				}
				//서버연동 성공.
				if(result.getString("flag").equals("0000")){
					msg.what = 1;
				}
				//연동 실패.
				else {
					msg.what = 9999;
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			mHandler.sendMessage(msg);
		}
	}

	class getMapDataTask extends AsyncTask<JSONObject, Void, JSONObject>{
		
		private ArrayList<MapBean> mList;
		
		public getMapDataTask(ArrayList<MapBean> mList) {
			// TODO Auto-generated constructor stub
			this.mList = mList;
		}
		
		@Override
		protected JSONObject doInBackground(JSONObject... params) {
			// TODO Auto-generated method stub
			JSONObject data = CodeUtil.getInstance(mContext).getHttpClient(params[0]);
			return data;
		}
		
		@Override
		protected void onPostExecute(JSONObject result) {
			// TODO Auto-generated method stub
//			super.onPostExecute(result);
			Message msg = new Message();
			try {
				if(result == null){
					msg.what = 2000;
					mHandler.sendMessage(msg);
					return;
				}
				//서버연동 성공.
				if(result.getString("flag").equals("0000")){
					Log.d("TEST", "result : "+result.toString());
					for(int i=0; i<result.getJSONArray("list_data").length(); i++){
						JSONObject mData = result.getJSONArray("list_data").getJSONObject(i);
						MapBean mBean = new MapBean();
						mBean.setSums(mData.getDouble("sums"));
						mBean.setM_country(mData.getString("m_country"));
						mBean.setName(mData.getString("name"));
						mBean.setLat(mData.getDouble("lat"));
						mBean.setLng(mData.getDouble("lng"));
						mList.add(mBean);
					}
					msg.what = 1;
				}
				//연동 실패.
				else {
					msg.what = 9999;
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			mHandler.sendMessage(msg);
		}
	}
}
